/**
 * 递归函数
 */
export function treeFn(arr, id, list) {
  arr.forEach((item) => {
    item.value = item.id;
    item.label = item.name;
    item.checked = false;
    if (item.parentId == id) {
      list.push(item);
    }
  });
  list.forEach((item) => {
    item.children = [];
    treeFn(arr, item.id, item.children);
    if (item.children.length < 1) {
      delete item.children;
    }
  });
  return list;
}
/**
* 数组对象去重
* @param {array} arr 去重的数组对象 [{title: 1}, {title: 2}, {title: 2}]
* @param {string} field 去重时比对的字段 title
* @returns
*/
const setArr = (arr, field) => {
  // 改变原数组
  // for (var i = 0; i < arr.length - 1; i++) {
  //   for (var j = i + 1; j < arr.length; j++) {
  //     if (arr[i][field] == arr[j][field]) {
  //       arr.splice(j, 1);
  //       //因为数组长度减小1，所以直接 j++ 会漏掉一个元素，所以要 j--
  //       j--;
  //     }
  //   }
  // }
  // return arr;
  // 不改变原数组
  let temp = [];
  arr.forEach(function (a) {
    var check = temp.every(function (b) {
      return a[field] !== b[field];
    });
    check ? temp.push(a) : "";
  });
  return temp;
};
/**
* 保留字段 非公用方法
* @param {array} arr 数组
* @returns
*/
const holdChapterField = (arr) => {
  if (arr.length > 0) {
    return arr.map(({ chapter, subsection }) => {
      return { chapter, subsection, checked: false, children: [] };
    });
  } else {
    return [];
  }
};
/**
 * 栏目树
 * @param {array} arr 数组 
 * @returns 
 */
export function menuTree(arr) {
  let chapterArr = setArr(arr, "chapter"); // 章节
  let chapterArrNow = holdChapterField(chapterArr, true);
  chapterArrNow.forEach((item) => {
    let children = [];
    if (arr) {
      arr.forEach((ite) => {
        if (ite.chapter === item.chapter) {
          // 小节
          children.push(ite);
        }
      });
    }
    let childrenNow = setArr(children, "subsection");
    item.children = holdChapterField(childrenNow, true);
    if (item.children) {
      item.children.forEach((value) => {
        arr.forEach((v) => {
          if (
            item.chapter === v.chapter &&
            value.subsection === v.subsection
          ) {
            // 初始化progress
            v.progress = {
              courseVideoId: v.progress ? v.progress.courseVideoId : "",
              status: v.progress ? v.progress.status : 0,
              currentDuration: v.progress
                ? v.progress.currentDuration
                : 0,
            };
            // 问答排序
            if (v.verifyQuestions && v.verifyQuestions.length > 0) {
              v.verifyQuestions.sort((a, b) => {
                return a.second - b.second;
              });
            }
            // 课程
            value.children.push(v);
          }
        });
      });
    }
  });
  return chapterArrNow
}

/**
* 秒转换成时分秒
* @param {number} seconds 需要转换的秒数 
* @returns 时：分：秒
*/
export function convertSecondsToHMS(seconds, format = 'H:i:s') {
  var hours = Math.floor(seconds / 3600);
  var minutes = Math.floor((seconds % 3600) / 60);
  var remainingSeconds = parseInt(seconds % 60);
  hours = hours < 10 ? '0' + hours : hours;
  minutes = minutes < 10 ? '0' + minutes : minutes;
  remainingSeconds = remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds;
  if (hours > 0) {
    // return hours + ":" + minutes + ":" + remainingSeconds;
    return format.replace('H', hours).replace('i', minutes).replace('s', remainingSeconds)
  }
  // return minutes + ":" + remainingSeconds;
  return format.replace('H:', '').replace('i', minutes).replace('s', remainingSeconds)
}

// 展开全屏
export function enterFullScreen() {
  let element = document.documentElement;
  if (element.requestFullscreen) {
    element.requestFullscreen();
  } else if (element.mozRequestFullScreen) {
    /* Firefox */
    element.mozRequestFullScreen();
  } else if (element.webkitRequestFullscreen) {
    /* Chrome, Safari & Opera */
    element.webkitRequestFullscreen();
  } else if (element.msRequestFullscreen) {
    /* IE/Edge */
    element.msRequestFullscreen();
  }
  // 禁止屏幕滚动
  document.body.classList.add('el-popup-parent--hidden')
}
// 退出全屏
export function exitFullScreen() {
  if (document.exitFullscreen) {
    document.exitFullscreen();
  } else if (document.mozCancelFullScreen) {
    /* Firefox */
    document.mozCancelFullScreen();
  } else if (document.webkitExitFullscreen) {
    /* Chrome, Safari and Opera */
    document.webkitExitFullscreen();
  } else if (document.msExitFullscreen) {
    /* IE/Edge */
    document.msExitFullscreen();
  }
  // 解除屏幕滚动
  document.body.classList.remove('el-popup-parent--hidden')
}
/**
 * 时间戳转化为年 月 日 时 分 秒
 * @param {number} number: 传入时间戳
 * @param {string} format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
export const formatTime = (number, format = 'Y-m-d H:i:s') => {
  var formateArr = ['Y', 'm', 'd', 'H', 'i', 's'];
  var returnArr = [];
  var date = new Date(number);
  // var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// 判断是否为空
export const isEmpty = (value) => {
  return (value == null || value == undefined || value == NaN) ? true : false;
}